package com.liaogui.web;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author liaogui
 * @date 2025/1/10 09:41
 */
@RestController
@RequestMapping("/exception/")
public class ExceptionController {
    @RequestMapping("test1")
    public String test1() {
        int i = 1 / 0;
        return "liaogui";
    }

    /**
     * 异常处理程序
     *
     * @param ex    异常对象
     * @param model 模型对象
     * @return {@link String}
     */
    @ExceptionHandler({java.lang.NullPointerException.class, java.lang.ArithmeticException.class})
    public String exceptionHandler1(Exception ex, Model model){
        // 向模型中添加异常对象
        model.addAttribute("msg",ex);
        return "error";
    }

}
